//function for validating forms
function validateForm(){
    if($('.validate-form')[0])
        $('.validate-form').each(function(){
            
            $(this).validate({
                errorClass: "help-block",
                errorElement: "span",
                highlight: function (element, errorClass, validClass) {
                    $(element).closest('.form-group').addClass('has-error');
                },
                unhighlight: function (element, errorClass, validClass) {
                    $(element).closest('.form-group').removeClass('has-error');
                },
                errorPlacement: function (error, element) {
                    if (element.parent('.input-group').length || element.prop('type') === 'checkbox' || element.prop('type') === 'radio') {
                        error.insertAfter(element.parent());
                    } else {
                        error.insertAfter(element);
                    }
                },
                invalidHandler: function(form, validator) {
                    var errors = validator.numberOfInvalids();
                    if (errors) {
                        var message = errors == 1
                        ? 'You missed 1 field. It has been highlighted'
                        : 'You missed ' + errors + ' fields. They have been highlighted';
                        $('#middle').prepend('<p class="bg-danger flashmessenger">'+message+'</p>'); 
                    
                        setTimeout(function() {
                            $('.flashmessenger').fadeOut('slow');
                        }, 3000);

                    
                    }
                }
            
            });
            
        });
}

function ratyPlugin(){
    $('.raty').raty({
        starOff: baseUrl + '/assets/raty/images/star-off.png',
        starOn: baseUrl + '/assets/raty/images/star-on.png',
        score: function() {
            return $(this).attr('data-score');
        }
    });

}

function flashmessenger(type, message){
    var type_class = 'bg-'+type;
    $('.'+type_class+'.flashmessenger').empty().html('&nbsp;'+message);
    if($('.'+type_class+'.flashmessenger')[0]){
        $('.'+type_class+'.flashmessenger').css('display','block');
    }
    else{
        $('#middle').prepend('<p class="'+type_class+' flashmessenger">&nbsp;'+message+'</p>');
    }
    setTimeout(function() {
        $('.flashmessenger').fadeOut('slow');
    }, 3000);
                
                
}
function incrementQuantity(_this){
    $this = $(_this);
    var limit = $this.data('quantity');
    var prod_quant = $(".prod-quant-input").val();
        
    if(prod_quant < limit){
        prod_quant = parseInt(prod_quant) + 1;
        $(".prod-quant-input").val(prod_quant);
            
    }else if(prod_quant == limit){
        alert('Sorry!! We have limited quantity for this product. Thanks!!');
    }
}
function decrementQuantity(_this){
    $this = $(_this);
    var limit = $this.data('quantity');
    var prod_quant = $(".prod-quant-input").val();
        
    if(limit == 0){
        alert("Sorry!! This product is out of stock!!");
        return false;
    }
        
    if (prod_quant == 1)
    {
        prod_quant = 1;
    }
    else
    {
        prod_quant = parseInt(prod_quant) - 1;
    }
    $(".prod-quant-input").val(prod_quant);
}
    
function updatestock(size){
    //overlay 
    var qty = sizeQty[size];
    $('.prod-quant-dec-btn, .prod-quant-inc-btn').data('quantity',qty);
    if(qty > 0 && size)
    {
        $('.instock').empty().html('Instock');
        $('.prod-quant-input').val(1);
    }
    else{
        $('.instock').empty().html('Out of stock');
        $('.prod-quant-input').val(0);
    }
    //remove overlay after updating
}